/* Gluten Library -- Utility
 *
 * A grab bag of useful types and functionality. Anything that is too small to
 * have its own place will likely be stuffed here.
 *
 * $AUTHOR$    res
 * $UPDATE$    r39
 */

/* LICENSES ********************************************************************

Copyright (c) 2013- Reuben E. Smith

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*******************************************************************************/


#include <fstream>
#include <GL/glew.h>
#include <GL/freeglut.h>

#ifdef _MSC_VER
   #pragma comment(lib, "Winmm.lib")
   #include <MMSystem.h>
#else
   #include <ctime>
#endif

#include "Utility.hpp"


namespace Gluten
{
   const Color Color::RED(255, 0, 0, 255);
   const Color Color::GREEN(0, 255, 0, 255);
   const Color Color::BLUE(0, 0, 255, 255);
   const Color Color::YELLOW(255, 255, 0, 255);
   const Color Color::CYAN(0, 255, 255, 255);
   const Color Color::MAGENTA(255, 0, 255, 255);
   const Color Color::BLACK(0, 0, 0, 255);
   const Color Color::WHITE(255, 255, 255, 255);
   const Color Color::GREY(128, 128, 128, 255);


   template <>
   bool fpRelativeEqual(float a, float b)
   {
      static const float MAX_EPSILON = 10 * FLT_EPSILON;

      float dD = std::fabs(a - b);
      float dMax = std::max(a, b);

      if (dD > dMax * MAX_EPSILON) {
         return false;
      }

      return true;
   }


   template <>
   bool fpRelativeEqual(double a, double b)
   {
      static const double MAX_EPSILON = 10 * DBL_EPSILON;

      double dD = std::fabs(a - b);
      double dMax = std::max(a, b);

      if (dD > dMax * MAX_EPSILON) {
         return false;
      }

      return true;
   }


   std::vector< std::string > vectorizeFile(const std::string& path)
   {
      std::vector< std::string > r;
      std::ifstream file(path.c_str(), std::ios::in);

      if (!file.is_open()) {
         return r;
      }

      for (std::string l; std::getline(file, l); r.push_back(l));
      file.close();

      return r;
   }


   std::vector< std::string > tokenizeString(const std::string& str,
      const std::string& delim)
   {
      std::vector< std::string > r;

      for (size_t wA = 0, wB = 0, wL = 0; true;) {
         wA = str.find_first_not_of(delim, wB);

         if (wA == std::string::npos) {
            break;
         }

         wB = str.find_first_of(delim, wA);

         if (wB != std::string::npos) {
            wL = wB - wA;
         }
         else {
            wL = wB;
         }

         r.push_back(str.substr(wA, wL));
      }

      return r;
   }


   void drawTextAt(const std::string& text, void* font, double x, double y,
      const Color& color)
   {
      if (text.empty() || font == nullptr) {
         return;
      }

      glPushAttrib(GL_ALL_ATTRIB_BITS);
      glDisable(GL_DEPTH_TEST);
      glDisable(GL_FOG);
      glDisable(GL_LIGHTING);
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_TEXTURE_3D);

      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadIdentity();
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();

      gluOrtho2D(0, 1, 0, 1);
      glColor4ub(color.r, color.g, color.b, color.a);
      glRasterPos2d(x, y);
      const unsigned char* utextStr =
         reinterpret_cast<const unsigned char*>(text.c_str());
      glutBitmapString(font, utextStr);

      glPopMatrix();
      glPopMatrix();

      glPopAttrib();

      // res: Necessary to ignore errors from disabling possibly not-enabled
      //      things. It's faster to just try and fail than to check and do.
      while (glGetError() != GL_NO_ERROR);
   }


   uintmax_t getSystemTime()
   {
      uintmax_t r = 0;

   #ifdef _MSC_VER
      r = timeGetTime();
   #else
      timespec ts;

      if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0) {
         r = ts.tv_sec * 1000.0 + ts.tv_nsec / 1000000.0;
      }
   #endif

      return r;
   }


   uintmax_t getProgramTime()
   {
      uintmax_t r = 0;

   #ifdef _MSC_VER
      r = timeGetTime();
   #else
      timespec ts;

      if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0) {
         r = ts.tv_sec * 1000.0 + ts.tv_nsec / 1000000.0;
      }
   #endif

      return r - PROGRAM_START_TIME;
   }
}

